package com.bjx.common.data.scope;

import cn.hutool.core.util.StrUtil;
import com.bjx.common.data.scope.annotation.DataScope;
import com.bjx.common.security.domain.LoginUserVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc: 数据权限具体sql拼接
 * @author: liushuai
 * @date: 2022/10/22 14:25
 */
public class ScopeHandler {
    private static final String ORG_SON = "WITH RECURSIVE cte AS (SELECT o.org_id FROM sys_org AS o WHERE o.org_id IN ({}) UNION ALL SELECT r.org_id FROM cte JOIN sys_org AS r ON r.pid = cte.org_id ) SELECT distinct * FROM cte";
    private static final String COM_SON = "WITH RECURSIVE cte AS ( SELECT o.org_id, o.org_name, o.TYPE FROM sys_org AS o WHERE o.org_id in ({}) UNION ALL SELECT r.org_id, r.org_name, r.TYPE FROM cte JOIN sys_org AS r ON r.com_id = cte.org_id ) SELECT org_id FROM cte  WHERE cte.TYPE = 1";

    private static final List<String> END_LIST = new ArrayList<String>();

    static {
        END_LIST.add("ORDER");
        END_LIST.add("GROUP");
        END_LIST.add("HAVING");
    }

    /**
     * 当前用户
     *
     * @param loginUserVO
     * @param ids
     * @param dataScope
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 14:39
     */
    public String sql_1(LoginUserVO loginUserVO, List<Long> ids, DataScope dataScope) {
        String userIdStr = dataScope.userId();
        if(StrUtil.isEmpty(userIdStr)){
            return "";
        }
        return StrUtil.format(" OR {} = {}", userIdStr, loginUserVO.getUserId());
    }

    /**
     * 当前组织机构
     *
     * @param loginUserVO
     * @param ids
     * @param dataScope
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 14:42
     */
    public String sql_2(LoginUserVO loginUserVO, List<Long> ids, DataScope dataScope) {
        String orgIdStr = dataScope.orgId();
        if(StrUtil.isEmpty(orgIdStr)){
            return "";
        }
        String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining());
        return StrUtil.format(" OR {} in ({})", orgIdStr, idsStr);
    }

    /**
     * 当前组织机构以及子机构
     *
     * @param loginUserVO
     * @param ids
     * @param dataScope
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 14:42
     */
    public String sql_3(LoginUserVO loginUserVO, List<Long> ids, DataScope dataScope) {
        String orgIdStr = dataScope.orgId();
        if(StrUtil.isEmpty(orgIdStr)){
            return "";
        }
        String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining());
        return StrUtil.format(" OR {} in (" + ORG_SON + ")", orgIdStr, idsStr);
    }

    /**
     * 当前公司
     *
     * @param loginUserVO
     * @param ids
     * @param dataScope
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 14:42
     */
    public String sql_4(LoginUserVO loginUserVO, List<Long> ids, DataScope dataScope) {
        String comIdStr = dataScope.comId();
        if(StrUtil.isEmpty(comIdStr)){
            return "";
        }
        String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining());
        return StrUtil.format(" OR {} in ({})", comIdStr, idsStr);
    }

    /**
     * 当前公司以及子公司
     *
     * @param loginUserVO
     * @param ids
     * @param dataScope
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 14:42
     */
    public String sql_5(LoginUserVO loginUserVO, List<Long> ids, DataScope dataScope) {
        String comIdStr = dataScope.comId();
        if(StrUtil.isEmpty(comIdStr)){
            return "";
        }
        String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining());
        return StrUtil.format(" OR {} in (" + COM_SON + ")", comIdStr, idsStr);
    }


    /**
     * 合并原始sql和合并sql
     *
     * @param originalSql
     * @param sql
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/10/22 15:47
     */
    public static String SqlMerge(String originalSql, String sql) {
        if (StrUtil.isEmpty(sql)) {
            return originalSql;
        }
        int index = -1;
        for (String value : END_LIST) {
            int i = StrUtil.indexOfIgnoreCase(originalSql, value);
            index = i == -1? index : i;
        }
        //判断是否有where条件
        int whereIndex = StrUtil.indexOfIgnoreCase(originalSql, "where");
        if (whereIndex == -1) {
            sql = " WHERE " + sql;
        } else {
            sql = " AND (" + sql + ")";
        }
        //没有结束符号
        if (index == -1) {
            originalSql += sql;
        } else {
            originalSql = originalSql.substring(0, index) + " " + sql + " " + originalSql.substring(index);
        }
        return originalSql;
    }


}
